package com.canyou.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Model;

/**
 * 地区表
 * <p>
 * [Id] ,[ParentId] ,[AreaName] ,[Layer]
 * </p>
 * 
 * @created cgz 2012-12-20
 */
public class Area extends Model<Area> {

	private static final long serialVersionUID = 6388234430977383678L;

	private static Logger logger = Logger.getLogger(Area.class);

	public static final Area dao = new Area();

	/**
	 * 根据上一级地区Id获取下一级地区列表
	 * 
	 * @author cgz 2012-12-19
	 * @param pid
	 *            上一级地区Id
	 * @return 下一级地区列表
	 */
	public List<Area> getListById(int pid) {
		List<Area> areas = new ArrayList<Area>();
		if (pid > -1) {
			try {
				areas = dao.find(
						"select * from Public_AreaDatum_tb where ParentId=?",
						pid);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return areas;
	}

	/**
	 * 根据Id获取地区名称
	 * 
	 * @author cgz 2012-12-19
	 * @param id
	 *            地区Id
	 * @return 地区名称
	 */
	public String getNameById(int id) {
		Area area = null;
		if (id > -1) {
			try {
				area = findById(id, "AreaName");
			} catch (ActiveRecordException e) {
				logger.error(e.getMessage(), e);
			}
		}
		return null == area ? "未知" : area.getStr("AreaName");
	}
}
